package com.alatus.springAI.Controller;

import com.alatus.springAI.Bean.*;
import com.alatus.springAI.SpringAiApplication;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.alatus.springAI.Service.ExcelService;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class ChatController {
    @Autowired
    private ExcelService excelService;
    @GetMapping(value = "/api/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 读取Excel文件
        List<WalmartProduct> originList = EasyExcel.read(ChatController.class.getClassLoader().getResourceAsStream("Template.xlsx")).sheet().doReadSync();
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("沃尔玛文件","utf-8")+System.currentTimeMillis() +".xlsx");
        List<WalmartProduct> dataList = excelService.getSkuInfo();
        originList.addAll(dataList);
        EasyExcel.write(response.getOutputStream(), WalmartProduct.class).sheet("Walmart Products").doWrite(dataList);
    }
    @GetMapping(value = "/api/exportInventory")
    public void exportInventory(HttpServletResponse response) throws IOException {
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("Inventory","utf-8")+System.currentTimeMillis() +".xlsx");
        List<InventoryUpdateData> dataList = excelService.getInventoryUpdateData();
        EasyExcel.write(response.getOutputStream(), InventoryUpdateData.class)
                .sheet()
                .doWrite(dataList);
    }
    @GetMapping(value = "/api/exportPricePromoData")
    public void exportOrderPricePromoData(HttpServletResponse response) throws IOException {
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("Price_New","utf-8")+System.currentTimeMillis() +".xlsx");
        List<PricePromoData> dataList = excelService.getPricePromoData();
        EasyExcel.write(response.getOutputStream(), PricePromoData.class)
                .sheet()
                .doWrite(dataList);
    }
    @GetMapping(value = "/api/exportSkuLagTimeData")
    public void exportSkuLagTimeData(HttpServletResponse response) throws IOException {
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("Lag-Time","utf-8")+System.currentTimeMillis() +".xlsx");
        List<SkuLagTimeData> dataList = excelService.getSkuLagTimeData();
        EasyExcel.write(response.getOutputStream(), SkuLagTimeData.class)
                .sheet()
                .doWrite(dataList);
    }
    @GetMapping(value = "/api/exportProductReturnStatus")
    public void exportProductReturnStatus(HttpServletResponse response) throws IOException {
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("BulkUpload_Returns_ItemOverrides_v5","utf-8")+System.currentTimeMillis() +".xlsx");
        List<ProductReturnStatus> dataList = excelService.getProductReturnStatus();
        EasyExcel.write(response.getOutputStream(), ProductReturnStatus.class)
                .sheet()
                .doWrite(dataList);
    }
    @GetMapping(value = "/api/exportSkuShippingTemplateData")
    public void exportSkuShippingTemplateData(HttpServletResponse response) throws IOException {
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("SKU_Template","utf-8")+System.currentTimeMillis() +".xlsx");
        List<SkuShippingTemplateData> dataList = excelService.getSkuShippingTemplateData();
        EasyExcel.write(response.getOutputStream(), SkuShippingTemplateData.class)
                .sheet()
                .doWrite(dataList);
    }
    @GetMapping(value = "/api/exportWalmartPromoData")
    public void exportWalmartPromoData(HttpServletResponse response) throws IOException {
        //要想让浏览器弹出下载框，后端要设置一下响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("X-Frame-Options", "allow-from uri");// 解决IFrame拒绝的问题
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("Promotion","utf-8")+System.currentTimeMillis() +".xlsx");
        List<WalmartPromoData> dataList = excelService.getWalmartPromoData();
        EasyExcel.write(response.getOutputStream(), WalmartPromoData.class)
                .sheet()
                .doWrite(dataList);
    }
    @GetMapping("/")
    public String hello(){
        return "updata";
    }
    @PostMapping(value = "/api/sku/importExcel")
    public String importExcel(@RequestPart("skuFile") MultipartFile skuFile,
                              @RequestPart("shopId") String shopId,
                              @RequestPart("shopName") String shopName) throws IOException {
//        接收到的文件名字需要和前端提交的名字相同,否则无法接收
        SpringAiApplication.shopId = shopId;
        SpringAiApplication.shopName= shopName;
        excelService.importExcel(skuFile.getInputStream());
        return "getData";
    }
}
